<?php
// +----------------------------------------------------------------------
// | Description: 用户组
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use app\admin\model\Common;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;

class Demand extends Common
{
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如微信模块用weixin作为数据表前缀
     */
    protected $name = 'admin_bidding_demand';

    /**
     * [getDataList 获取列表]
     * @linchuangbin
     * @DateTime  2017-02-10T21:07:18+0800
     * @return    [array]
     */

    public function getDataList($page, $limit, $param)
    {
        $map = [];
        if ($param['bid_id']) {
            $map['bid_id'] = $param['bid_id'];
        } else {
            return resultArray(['error' => '发生致命错误 ErrorCode：0x00001']);
        }
        $dataCount = $this->alias('admin_bidding_demand')->where($map)->count('id');
        $data['list'] = $this->where($map)->page($page, $limit)->select();
        $data['dataCount'] = $dataCount;
        return $data;
    }

    public function multipleSave($file, $bid_id)
    {
        $path = $file->getRealPath();
        setlocale(LC_ALL, 'zh_CN');
        $inputFileType = IOFactory::identify($path);
        $excelReader = IOFactory::createReader($inputFileType);
        if ($inputFileType == 'Csv') {   //csv文件读取设置
            $excelReader->setInputEncoding('GBK');
            $excelReader->setDelimiter(',');
        }
        $phpExcel = $excelReader->load($path);
        $activeSheet = $phpExcel->getActiveSheet();
        $sheet = $activeSheet->toArray();
        unset($sheet[0]);
        $param = [];
        $this->startTrans();
        try {
            foreach ($sheet as $key => $value) {
                $param[$key]['bid_id'] = $bid_id;
                $param[$key]['name'] = $value[0];
                $param[$key]['unit'] = $value[1];
                $param[$key]['model'] = $value[2];
                $param[$key]['delivery_address'] = $value[3];
                $param[$key]['num'] = $value[4];
                $param[$key]['benchmark_price'] = $value[5];
                $param[$key]['material'] = $value[6];
                $param[$key]['vender'] = $value[7];
                $param[$key]['brand'] = $value[8];
                $param[$key]['remark'] = $value[9];
            }
            if (!empty($param)) {
                $this->insertAll($param);
            }
            $this->commit();
        } catch (\Exception $e) {
            var_dump($e);
            $this->rollback();
            $this->error = '添加失败';
            return false;
        }
        return true;
    }
    public function ext($param)
    {
        if(empty($param['bid_id'])){
            $this->error = '发生致命错误！';
            return false;
        }
        $demandExt=Db::name('admin_bidding_demand_ext');
        $data=$demandExt->where(array('bid_id'=>$param['bid_id']))->find();
        if(!$data){
            return [];
        }
        foreach ($data as $key => $value){
            if($key=='bid_id'||$key=='id')continue;
            $data[$key]=boolval($value);
        }
        return $data;
    }
    public function updateExt($param)
    {
        if(empty($param['bid_id'])){
            $this->error = '添加失败';
            return false;
        }
        $demandExt=Db::name('admin_bidding_demand_ext');
        $exists=$demandExt->where(array('bid_id'=>$param['bid_id']))->find();
        if($exists){
            $param['id']=$exists['id'];
            $demandExt->where(array('bid_id'=>$param['bid_id']))->update($param);
            $res=true;
        }else{
            $res=$demandExt->insert($param);
        }
        if(!$res){
            $this->error = '添加失败';
            return false;
        }
        return true;
    }
}